package com.cya.forumproject.exception;

import com.cya.forumproject.common.AppResult;
import com.cya.forumproject.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/9/19 20:03
 * @description：
 * @modified By：
 * @version:
 */
//同意返回异常
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult applicationException(ApplicationException e){
     log.error(e.getMessage());
     if (e.getErrorResult()!=null){
         return e.getErrorResult();
     }
     if (e.getMessage()==null||e.getMessage().equals("")){
         return AppResult.failed(ResultCode.ERROR_SERVICES);
     }
     return AppResult.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult execptionHandler(Exception e){

        e.printStackTrace();
        log.error(e.getMessage());
        if (e.getMessage()==null||e.getMessage().equals("")){
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.failed(e.getMessage());
    }
}